{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "pcb_web.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "aXzFIRogQWX4",
        "outputId": "58df8d02-de45-4d39-a44c-743f507039c5"
      },
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Mounted at /content/drive\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-89Ei6P7Ql1w",
        "outputId": "d26d2dc0-ca5f-4689-8989-437eb4725eb9"
      },
      "source": [
        "!ln -s /content/drive/My\\ Drive/ /mydrive\n",
        "%cd '/mydrive/PCB_project/darknet'\n",
        "!ls"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "/content/drive/My Drive/PCB_project/darknet\n",
            "00041000.jpg  00041039.jpg\t cfg\t\t\t  image_yolov4.sh\n",
            "00041002.jpg  00041041.jpg\t chart.png\t\t  include\n",
            "00041003.jpg  00041044.jpg\t chart_yolov3_custom.png  json_mjpeg_streams.sh\n",
            "00041005.jpg  00041045.jpg\t cmake\t\t\t  LICENSE\n",
            "00041006.jpg  00041048.jpg\t CMakeLists.txt\t\t  Makefile\n",
            "00041007.jpg  00041060.jpg\t darknet\t\t  net_cam_v3.sh\n",
            "00041009.jpg  00041149.jpg\t darknet53.conv.74\t  net_cam_v4.sh\n",
            "00041010.jpg  12000001_temp.jpg  darknet53.conv.74.1\t  obj\n",
            "00041011.jpg  12000001_test.jpg  DarknetConfig.cmake.in   predictions.jpg\n",
            "00041012.jpg  13000098_temp.jpg  darknet_images.py\t  README.md\n",
            "00041013.jpg  13000098_test.jpg  darknet.py\t\t  results\n",
            "00041014.jpg  3rdparty\t\t darknet_video.py\t  scripts\n",
            "00041027.jpg  backup\t\t data\t\t\t  src\n",
            "00041030.jpg  bad.list\t\t dataset\t\t  vcpkg.json\n",
            "00041031.jpg  build\t\t generate_train.py\t  video_yolov3.sh\n",
            "00041032.jpg  build.ps1\t\t image_yolov3.sh\t  video_yolov4.sh\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "v_T8NumyRfNz"
      },
      "source": [
        "!chmod +x ./darknet"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MQ5x8VF3QPHH",
        "outputId": "814948a3-eb5e-4414-ea96-5be227d9b139"
      },
      "source": [
        "!pip install pywebio\n",
        "!pip install flask_ngrok"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Collecting pywebio\n",
            "  Downloading pywebio-1.4.0.tar.gz (339 kB)\n",
            "\u001b[K     |████████████████████████████████| 339 kB 5.4 MB/s \n",
            "\u001b[?25hRequirement already satisfied: tornado>=5.0 in /usr/local/lib/python3.7/dist-packages (from pywebio) (5.1.1)\n",
            "Collecting user-agents\n",
            "  Downloading user_agents-2.2.0-py3-none-any.whl (9.6 kB)\n",
            "Collecting ua-parser>=0.10.0\n",
            "  Downloading ua_parser-0.10.0-py2.py3-none-any.whl (35 kB)\n",
            "Building wheels for collected packages: pywebio\n",
            "  Building wheel for pywebio (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for pywebio: filename=pywebio-1.4.0-py3-none-any.whl size=348394 sha256=9596a3cb3c804b500f7cc64b80f520a6476fad7eaf722eace07bef9a9082813e\n",
            "  Stored in directory: /root/.cache/pip/wheels/db/7c/3b/853abdef49e39ea18d115f0262ee9e4c0a344bf1ba5439390e\n",
            "Successfully built pywebio\n",
            "Installing collected packages: ua-parser, user-agents, pywebio\n",
            "Successfully installed pywebio-1.4.0 ua-parser-0.10.0 user-agents-2.2.0\n",
            "Collecting flask_ngrok\n",
            "  Downloading flask_ngrok-0.0.25-py3-none-any.whl (3.1 kB)\n",
            "Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from flask_ngrok) (2.23.0)\n",
            "Requirement already satisfied: Flask>=0.8 in /usr/local/lib/python3.7/dist-packages (from flask_ngrok) (1.1.4)\n",
            "Requirement already satisfied: Werkzeug<2.0,>=0.15 in /usr/local/lib/python3.7/dist-packages (from Flask>=0.8->flask_ngrok) (1.0.1)\n",
            "Requirement already satisfied: click<8.0,>=5.1 in /usr/local/lib/python3.7/dist-packages (from Flask>=0.8->flask_ngrok) (7.1.2)\n",
            "Requirement already satisfied: itsdangerous<2.0,>=0.24 in /usr/local/lib/python3.7/dist-packages (from Flask>=0.8->flask_ngrok) (1.1.0)\n",
            "Requirement already satisfied: Jinja2<3.0,>=2.10.1 in /usr/local/lib/python3.7/dist-packages (from Flask>=0.8->flask_ngrok) (2.11.3)\n",
            "Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.7/dist-packages (from Jinja2<3.0,>=2.10.1->Flask>=0.8->flask_ngrok) (2.0.1)\n",
            "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->flask_ngrok) (3.0.4)\n",
            "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->flask_ngrok) (2.10)\n",
            "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->flask_ngrok) (1.24.3)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->flask_ngrok) (2021.10.8)\n",
            "Installing collected packages: flask-ngrok\n",
            "Successfully installed flask-ngrok-0.0.25\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RclMAGasQPA3",
        "outputId": "9651010f-5e6a-4f41-e788-f76f07ce214d"
      },
      "source": [
        "from pywebio.platform.flask import webio_view\n",
        "from pywebio import STATIC_PATH\n",
        "from flask_ngrok import run_with_ngrok\n",
        "from flask import Flask, send_from_directory\n",
        "from pywebio.input import *\n",
        "from pywebio.output import *\n",
        "import argparse\n",
        "from pywebio import start_server\n",
        "import requests\n",
        "import json\n",
        "from bs4 import BeautifulSoup\n",
        "from sklearn.preprocessing import LabelEncoder\n",
        "import subprocess\n",
        "app = Flask(__name__)\n",
        "run_with_ngrok(app)   \n",
        "\n",
        "\n",
        "def predict():\n",
        "    img = file_upload(\"Select a image:\", accept=\"image/*\")\n",
        "\n",
        "    open(img['filename'], 'wb').write(img['content'])\n",
        "    src_img_loc = str(img['filename'])\n",
        "    des_img_loc = 'predictions.jpg'\n",
        "\n",
        "    # !./darknet detector test data/custom.data cfg/yolov3-tiny-custom.cfg /mydrive/modelbackup/yolov3-tiny-custom_last.weights /mydrive/pred2.jpg -thresh 0.3\n",
        "    # cmd = (['./darknet', 'detector', 'test', 'data/custom.data', 'cfg/yolov3-tiny-custom.cfg', '/mydrive/modelbackup/yolov3-tiny-custom_last.weights', src_img_loc, '-thresh 0.3'])\n",
        "    cmd = (['./darknet', 'detector', 'test', 'data/custom.data', 'cfg/yolov3-tiny-custom.cfg', '/mydrive/PCB_project/darknet/backup/yolov3_custom_final.weights', src_img_loc, '-thresh 0.4'])\n",
        "    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n",
        "\n",
        "    o, e = proc.communicate()\n",
        "\n",
        "    put_row([\n",
        "        put_image(open(src_img_loc, 'rb').read())\n",
        "        , None\n",
        "        , put_image(open(des_img_loc, 'rb').read())\n",
        "    ])\n",
        "\n",
        "app.add_url_rule('/', 'webio_view', webio_view(predict),\n",
        "            methods=['GET', 'POST', 'OPTIONS'])\n",
        "\n",
        "app.run()\n"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            " * Serving Flask app \"__main__\" (lazy loading)\n",
            " * Environment: production\n",
            "\u001b[31m   WARNING: This is a development server. Do not use it in a production deployment.\u001b[0m\n",
            "\u001b[2m   Use a production WSGI server instead.\u001b[0m\n",
            " * Debug mode: off\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            " * Running on http://3b64-34-133-166-255.ngrok.io\n",
            " * Traffic stats available on http://127.0.0.1:4040\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "127.0.0.1 - - [17/Nov/2021 04:03:33] \"\u001b[37mGET / HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:34] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:35] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:36] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:37] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:38] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:39] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:40] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:41] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:42] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:42] \"\u001b[37mPOST /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:44] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:45] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:46] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:47] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:53] \"\u001b[37mGET / HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:53] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:55] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n",
            "127.0.0.1 - - [17/Nov/2021 04:03:56] \"\u001b[37mGET /?app=index HTTP/1.1\u001b[0m\" 200 -\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1cCvHDp_QO-B",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "fde8d0d1-7dea-4a2f-9c7d-096b92593515"
      },
      "source": [
        "!./darknet detector test data/custom.data cfg/yolov3-tiny-custom.cfg /mydrive/PCB_project/darknet/backup/yolov3_custom_final.weights /mydrive/pred.jpg -thresh 0.4"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            " CUDA-version: 11010 (11020), cuDNN: 7.6.5, GPU count: 1  \n",
            " OpenCV version: 3.2.0\n",
            " 0 : compute_capability = 370, cudnn_half = 0, GPU: Tesla K80 \n",
            "net.optimized_memory = 0 \n",
            "mini_batch = 1, batch = 2, time_steps = 1, train = 0 \n",
            "   layer   filters  size/strd(dil)      input                output\n",
            "   0 Create CUDA-stream - 0 \n",
            " Create cudnn-handle 0 \n",
            "conv     16       3 x 3/ 1    416 x 416 x   3 ->  416 x 416 x  16 0.150 BF\n",
            "   1 max                2x 2/ 2    416 x 416 x  16 ->  208 x 208 x  16 0.003 BF\n",
            "   2 conv     32       3 x 3/ 1    208 x 208 x  16 ->  208 x 208 x  32 0.399 BF\n",
            "   3 max                2x 2/ 2    208 x 208 x  32 ->  104 x 104 x  32 0.001 BF\n",
            "   4 conv     64       3 x 3/ 1    104 x 104 x  32 ->  104 x 104 x  64 0.399 BF\n",
            "   5 max                2x 2/ 2    104 x 104 x  64 ->   52 x  52 x  64 0.001 BF\n",
            "   6 conv    128       3 x 3/ 1     52 x  52 x  64 ->   52 x  52 x 128 0.399 BF\n",
            "   7 max                2x 2/ 2     52 x  52 x 128 ->   26 x  26 x 128 0.000 BF\n",
            "   8 conv    256       3 x 3/ 1     26 x  26 x 128 ->   26 x  26 x 256 0.399 BF\n",
            "   9 max                2x 2/ 2     26 x  26 x 256 ->   13 x  13 x 256 0.000 BF\n",
            "  10 conv    512       3 x 3/ 1     13 x  13 x 256 ->   13 x  13 x 512 0.399 BF\n",
            "  11 max                2x 2/ 1     13 x  13 x 512 ->   13 x  13 x 512 0.000 BF\n",
            "  12 conv   1024       3 x 3/ 1     13 x  13 x 512 ->   13 x  13 x1024 1.595 BF\n",
            "  13 conv    256       1 x 1/ 1     13 x  13 x1024 ->   13 x  13 x 256 0.089 BF\n",
            "  14 conv    512       3 x 3/ 1     13 x  13 x 256 ->   13 x  13 x 512 0.399 BF\n",
            "  15 conv     33       1 x 1/ 1     13 x  13 x 512 ->   13 x  13 x  33 0.006 BF\n",
            "  16 yolo\n",
            "[yolo] params: iou loss: mse (2), iou_norm: 0.75, obj_norm: 1.00, cls_norm: 1.00, delta_norm: 1.00, scale_x_y: 1.00\n",
            "  17 route  13 \t\t                           ->   13 x  13 x 256 \n",
            "  18 conv    128       1 x 1/ 1     13 x  13 x 256 ->   13 x  13 x 128 0.011 BF\n",
            "  19 upsample                 2x    13 x  13 x 128 ->   26 x  26 x 128\n",
            "  20 route  19 8 \t                           ->   26 x  26 x 384 \n",
            "  21 conv    256       3 x 3/ 1     26 x  26 x 384 ->   26 x  26 x 256 1.196 BF\n",
            "  22 conv     33       1 x 1/ 1     26 x  26 x 256 ->   26 x  26 x  33 0.011 BF\n",
            "  23 yolo\n",
            "[yolo] params: iou loss: mse (2), iou_norm: 0.75, obj_norm: 1.00, cls_norm: 1.00, delta_norm: 1.00, scale_x_y: 1.00\n",
            "Total BFLOPS 5.455 \n",
            "avg_outputs = 325902 \n",
            " Allocate additional workspace_size = 12.46 MB \n",
            "Loading weights from /mydrive/PCB_project/darknet/backup/yolov3_custom_final.weights...\n",
            " seen 64, trained: 24 K-images (0 Kilo-batches_64) \n",
            "Done! Loaded 24 layers from weights-file \n",
            " Detection layer: 16 - type = 28 \n",
            " Detection layer: 23 - type = 28 \n",
            "/mydrive/pred.jpg: Predicted in 12.221000 milli-seconds.\n",
            "pin-hole: 60%\n",
            "short: 42%\n",
            "copper: 98%\n",
            "open: 46%\n",
            "pin-hole: 91%\n",
            "pin-hole: 72%\n",
            "Unable to init server: Could not connect: Connection refused\n",
            "\n",
            "(predictions:1430): Gtk-\u001b[1;33mWARNING\u001b[0m **: \u001b[34m05:30:34.512\u001b[0m: cannot open display: \n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XdCUp5cHAC32"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}